using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Management;

public partial class InstanceSysProcesses : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrid();
            LoadGridLocks();
        }
    }
    protected void LoadGrid()
    {
        SqlConnection cn = new SqlConnection(@"server=" + Request.QueryString["InstanceName"].ToString() + ";database=master;Integrated Security=SSPI;Pooling=False");
        cn.Open();

        SqlDataAdapter ad = new SqlDataAdapter(@"select p.spid,p.blocked,d.name as databasename,p.cpu,p.physical_io,p.memusage,p.waittime,REPLACE(p.hostname,':','-') as hostname,p.loginame from master.dbo.sysprocesses p INNER JOIN master.dbo.sysdatabases d on p.dbid = d.dbid", cn);
        DataSet ds = new DataSet();

        ad.Fill(ds, "data");

        this.gv.DataSource = ds;
        this.gv.DataBind();

        cn.Close();
    }

    protected void LoadGridLocks()
    {
        SqlConnection cn1 = new SqlConnection(@"server=" + Request.QueryString["InstanceName"].ToString() + ";database=master;Integrated Security=SSPI;Pooling=False");
        cn1.Open();

        SqlDataAdapter ad1 = new SqlDataAdapter(@"select getdate() 'asOfDate',req_spid as 'spid',	db_name(rsc_dbid) as 'database',object_name(rsc_objid) as 'object',case req_mode when 0 then NULL when 1 then 'Sch-S (Schema stability)' when 2 then 'Sch-M (Schema modification)' when 3 then 'S (Shared)' when 4 then 'U (Update)' when 5 then 'X (Exclusive)'when 6 then 'IS (Intent Shared)' when 7 then 'IU (Intent Update)' when 8 then 'IX (Intent Exclusive)' when 9 then  'SIU (Shared Intent Update)' when 10 then  'SIX (Shared Intent Exclusive)' when 11 then  'UIX (Update Intent Exclusive)' when 12 then  'BU. Used by bulk operations.' when 13 then  'RangeS_S (Shared Key-Range and Shared Resource lock)' when 14 then  'RangeS_U (Shared Key-Range and Update Resource lock)'when 15 then  'RangeI_N (Insert Key-Range and Null Resource lock)' when 16 then  'RangeI_S' when 17 then  'RangeI_U' when 18 then  'RangeI_X' when 19 then  'RangeX_S' when 20 then  'RangeX_U' when 21 then  'RangeX_X' end as 'lockType', case rsc_type when 1 then 'Resource (not used)' when 2 then 'Database' when 3 then 'File' when 4 then 'Index' when 5 then 'Table'	when 6 then 'Page' when 7 then 'Key' when 8 then 'Extent' when 9 then 'RID (Row ID)' when 10 then 'Application' end as 'resourceType' from master..syslockinfo", cn1);
        DataSet ds1 = new DataSet();

        ad1.Fill(ds1, "data");

        this.gvLocks.DataSource = ds1;
        this.gvLocks.DataBind();

        cn1.Close();
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }
    protected void btnRefreshLocks_Click(object sender, EventArgs e)
    {
        LoadGridLocks();
    }
}
